import sys
from PySide6.QtWidgets import (QDialog,QApplication, QWidget, QVBoxLayout, 
                              QCalendarWidget, QPushButton, QHBoxLayout)
from PySide6.QtGui import QTextCharFormat, QColor, QFont
from PySide6.QtCore import QDate, Qt, QLocale
from PySide6.QtCore import Signal, QDate  # 确保导入 Signal

class CustomDatePicker(QDialog):

    selected_date = Signal(QDate)
    def __init__(self,parent=None):
        super().__init__(parent=parent)
        self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)  # 确保置顶
        self.setModal(True)  # 设置为模态对话框
        self.initUI()        
        self.setWindowTitle("日期选择器")
        self.setGeometry(300, 300, 400, 400)

    def initUI(self):
        # 主布局
        main_layout = QVBoxLayout()
        
        # 创建并配置日历控件
        self.calendar = QCalendarWidget()
        self.calendar.setLocale(QLocale(QLocale.Chinese))  # 设置为中文显示
        self.calendar.setVerticalHeaderFormat(QCalendarWidget.NoVerticalHeader)  # 隐藏行号
        
        # 设置初始日期为2025年6月
        self.calendar.setCurrentPage(QDate.currentDate().year(), QDate.currentDate().month())
        self.calendar.setSelectedDate(QDate.currentDate())
        
        # 配置日历样式
        self.configureCalendarStyles()
        
        # 添加按钮布局
        button_layout = QHBoxLayout()
        button_layout.addStretch(1)  # 左侧弹性空间
        
        # 今天按钮
        today_btn = QPushButton("今天")
        today_btn.setFixedWidth(80)
        today_btn.setStyleSheet("""
            QPushButton {
                color: #1E90FF; 
                font-weight: bold;
                border: none;
                padding: 5px;
            }
            QPushButton:hover {
                text-decoration: underline;
            }
        """)
        today_btn.clicked.connect(self.showToday)
        
        button_layout.addStretch(1)  # 按钮间弹性空间
        button_layout.addWidget(today_btn)
        button_layout.addStretch(1)  # 右侧弹性空间
        
        # 添加到主布局
        main_layout.addWidget(self.calendar)
        main_layout.addLayout(button_layout)
        
        self.setLayout(main_layout)
        self.calendar.selectionChanged.connect(self.handleSelectionChanged)

    def configureCalendarStyles(self):
        """配置日历的各种样式效果"""
        # ===== 整体日历样式 =====
        self.calendar.setStyleSheet("""
            /* 整体日历样式 */
            QCalendarWidget {
                font-size: 14px;
                border: none;
                padding: 5px;
            }
            
            /* 导航栏样式 */
            QCalendarWidget QWidget#qt_calendar_navigationbar {
                background-color: #f5f5f5;
                height: 40px;
            }
            
            /* 月份/年份按钮 */
            QCalendarWidget QToolButton {
                background: transparent;
                font-weight: bold;
                font-size: 15px;
                color: black;
            }
            
            /* 表头（星期）样式 */
            QCalendarWidget QWidget#qt_calendar_calendarview QAbstractItemView {
                alternate-background-color: white;
            }
            
            QCalendarWidget QTableView#qt_calendar_calendarview {
                gridline-color: #e0e0e0;
            }
            
            /* 选中日期的样式 */
            QCalendarWidget QTableView#qt_calendar_calendarview::item:selected {
                background-color: white;
                border: 2px solid #000000;
                border-radius: 3px;
            }
            
            /* 当前月份的日期（更具体的选择器） */
            QCalendarWidget QTableView#qt_calendar_calendarview::item:enabled {
                color: black !important;
            }
            
            /* 其他月份的日期（更具体的选择器） */
            QCalendarWidget QTableView#qt_calendar_calendarview::item:!enabled {
                color: gray !important;
            }
        """)
        
        # ===== 表头星期文字样式 =====
        week_format = QTextCharFormat()
        week_format.setForeground(QColor("#333333"))
        week_format.setFontWeight(QFont.Bold)
        
        for i in range(1, 8):  # Monday = 1, Sunday = 7
            weekday = Qt.DayOfWeek(i)  # 显式转换为DayOfWeek枚举
            self.calendar.setWeekdayTextFormat(weekday, week_format)
        
        # 设置周末日期背景颜色
        weekend_format = QTextCharFormat()
        weekend_format.setForeground(QColor("red"))  # 设置红色字体
        
        # 最后设置当天日期的样式
        special_date_format = QTextCharFormat()
        special_date_format.setBackground(QColor("#1E90FF"))  # 深蓝色背景
        special_date_format.setForeground(Qt.white)  # 白色文字
        current_date = QDate.currentDate()
        self.calendar.setDateTextFormat(current_date, special_date_format)

    def clearSelection(self):
        """清除当前选中的日期"""
        # 移动到当前页面但没有选中的日期
        self.calendar.showToday()
        self.calendar.setSelectedDate(QDate())

    def showToday(self):
        """跳转到当前日期"""
        today = QDate.currentDate()
        self.calendar.setSelectedDate(today)
        self.calendar.setCurrentPage(today.year(), today.month())

    def handleSelectionChanged(self):
        selected_date = self.calendar.selectedDate()
        self.selected_date.emit(selected_date)  # 发射信号
        self.accept()  # 关闭窗口

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = CustomDatePicker()
    window.show()
    sys.exit(app.exec())